package com.ruoyi.system.controller;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Precords;
import com.ruoyi.system.service.IPrecordsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
/**
 * 车辆信息Controller
 */
@RestController
@RequestMapping("/system/precords")
public class PrecordsController extends BaseController {
    @Autowired
    private IPrecordsService precordsService;
    /**
     * 查询车辆信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:precords:list')")
    @GetMapping("/list")
    public TableDataInfo list(Precords precords,
                              @RequestParam(value = "startTime", required = false) String startTime,
                              @RequestParam(value = "endTime", required = false) String endTime,
                              @RequestParam(value = "cartype", required = false) String cartype) {
        startPage();
        QueryWrapper<Precords> queryWrapper = new QueryWrapper<>();

        // 如果提供了车牌号，则进行模糊匹配
        if (precords.getLicense() != null && !"".equals(precords.getLicense())) {
            queryWrapper.like("license", precords.getLicense());
        }

        // 如果提供了车辆类型，则精确匹配
        if (cartype != null && !cartype.isEmpty()) {
            queryWrapper.eq("cartype", cartype);
        }

        // 添加时间范围筛选，使用入场时间和出场时间
        if (startTime != null && !startTime.isEmpty()) {
            queryWrapper.ge("entertime", startTime); // 入场时间大于等于开始时间
        }
        if (endTime != null && !endTime.isEmpty()) {
            queryWrapper.le("outtime", endTime); // 出场时间小于等于结束时间
        }

        List<Precords> list = precordsService.list(queryWrapper);
        return getDataTable(list);
    }
    /**
     * 导出车辆信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:precords:export')")
    @Log(title = "车辆信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Precords precords) {
        QueryWrapper<Precords> queryWrapper = new QueryWrapper<>();
        if (precords.getLicense() != null && !"".equals(precords.getLicense())) {
            queryWrapper.like("license", precords.getLicense());
        }
        if (precords.getEntertime() != null && !"".equals(precords.getEntertime())) {
            queryWrapper.like("entertime", precords.getEntertime());
        }
        if (precords.getCartype() != null && !"".equals(precords.getCartype())) {
            queryWrapper.like("cartype", precords.getCartype());
        }
        List<Precords> list = precordsService.list(queryWrapper);
        ExcelUtil<Precords> util = new ExcelUtil<>(Precords.class);
        util.exportExcel(response, list, "车辆信息数据");
    }
    /**
     * 获取车辆信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:precords:query')")
    @GetMapping(value = "/{pid}")
    public AjaxResult getInfo(@PathVariable("pid") Long pid) {
        return success(precordsService.getById(pid));
    }
    /**
     * 新增车辆信息
     */
    @PreAuthorize("@ss.hasPermi('system:precords:add')")
    @Log(title = "车辆信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Precords precords) {
        return toAjax(precordsService.save(precords));
    }
    /**
     * 支付成功
     */
    @PreAuthorize("@ss.hasPermi('system:precords:edit')")
    @Log(title = "车辆信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Precords precords) {
        return toAjax(precordsService.updateById(precords));
    }
    /**
     * 删除车辆信息
     */
    @PreAuthorize("@ss.hasPermi('system:precords:remove')")
    @Log(title = "车辆信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{pids}")
    public AjaxResult remove(@PathVariable List<Long> pids) {
        return toAjax(precordsService.removeByIds(pids));
    }
}